<template lang="">
  <div>
    <!-- 搜索 -->
    <QueryForm :searchForm="searchForm" :items="items" @search="search" @list="list"></QueryForm>
    <!-- 操作按钮区域 -->
    <div class="btn-panel">
      <el-button type="success" size="small" icon="el-icon-plus" @click="addBtn">添加证书</el-button>
      <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteMany">批量删除</el-button>
    </div>
    <!-- 表格 -->
    <Table1 ref="multipleTable" :tableData="tableData" :tableCols="tableCols" @showEditView="showEditView()"
      @deleteUser="del">
      <template v-if="scope.data.resume!=null" slot="slot_resume" slot-scope="scope">
        {{scope.data.resume.name}}
      </template>

    </Table1>
    <div style="margin-top: 20px">
      <!-- 分页组件 -->
      <Page1 :pages="pages" :total="total" @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange"></Page1>

      <Add v-if="addParam.visible" :addParam="addParam" @addSave="addSave"></Add>
      <Edit v-if="editParam.visible" :editParam="editParam" @doUpdate="doUpdate"></Edit>
    </div>
  </div>
</template>
<script>
  import Add from "@/components/Add";
  import Edit from "@/components/Edit";
  import Table1 from "@/components/Table1";
  import Page1 from "@/components/Page1";
  import QueryForm from "@/components/QueryForm";
  import local from "@/store/local.js";

  export default {
    name: "certificate",
    components: {
      Add,
      Edit,
      Table1,
      Page1,
      QueryForm
    },
    data() {
      return {
        // 查询的表单
        searchForm: {
          //查询的表单
          name: "",
        },
        // 查询
        items: [{
          type: 'text',
          label: '证书名称：',
          name: 'name',
          placeholder: '按证书名称查询'
        }],
        // 表格
        total: 0,
        pages: 1,
        currentPage: 1,
        size: 10,
        tableData: [],

        tableCols: [{
            prop: 'resume_id',
            label: '简历',
            slot: 'slot_resume'
          },
          {
            prop: 'name',
            label: '证书名称'
          },
          {
            prop: 'obtain_date',
            label: '获得时间'
          },
          {
            prop: 'file',
            label: '附件'
          },
        ],

        paramItems: [{
            type: 'select',
            label: '简历',
            prop: 'resume_id',
            name: 'resume_id',
            placeholder: '请选择简历',
            options: []
          },
          {
            type: 'text',
            label: '证书名称',
            prop: 'name',
            name: 'name',
            placeholder: '请输入证书名称'
          },
          {
            type: 'file',
            label: '附件',
            prop: 'file',
            name: 'file',
            placeholder: '请选择附件'
          },
          {
            type: 'date',
            label: '获得时间',
            prop: 'obtain_date',
            name: 'obtain_date',
            placeholder: '请输入获得时间'
          },


        ],
        paramRules: {
          resume_id: [{
            required: true,
            message: '简历名称不能为空'
          }],
          name: [{
            required: true,
            message: '证书名称不能为空'
          }],
        },
        // 添加相关
        addParam: {
          title: "添加证书信息",
          visible: false,
          close: this.close,
          callback: this.list,
          addForm: {
            resume_id: '',
            name: '',
            file: '',
            obtain_date: '',
          },
          rules: {}, //表单规则
          items: [],
        },

        // 修改相关
        editParam: {
          title: "修改证书信息",
          visible: false,
          close: this.close,
          callback: this.list,
          editForm: null, //传递form表单数据
          rules: {}, //表单规则
          items: [],
        },
      };
    },



    created() {},
    mounted() {

      this.list();
      //赋值
      this.addParam.rules = JSON.parse(JSON.stringify(this.paramRules));
      this.editParam.rules = JSON.parse(JSON.stringify(this.paramRules));
      this.getResume()



    },
    methods: {
      //获取简历
      getResume(param) {
        this.postRequest("/resume/queryPage", {
          param
        }).then(res => {
          if (res) {
            let arr = [];
            res.data.forEach((item) => {
              let param = {
                value: item.id,
                label: item.name,
              };
              arr.push(param)
            });
            this.paramItems[0].options = arr
          }
          console.log(this.paramItems[0], " 简历");
        })
      },

      //初始化培训经历
      list(param) {
        this.postRequest("/certificate/queryPage", {
          param
        }).then((res) => {
          console.log(res, "分页查询的数据");
          this.tableData = res.data;
          this.total = res.total;
          this.pages = res.pages;

        });

      },
      // 当前页改变
      handleCurrentChange(page) {
        this.currentPage = page;
        this.list();
      },
      // 当前size改变
      handleSizeChange(size) {
        this.size = size;
        this.list();
      },
      // 查询
      search() {

        this.postRequest("/certificate/queryPage", this.searchForm).then((res) => {
          if (res) {
            console.log(res, "搜索后的数据");
            this.tableData = res.data;
          }
        });
      },
      //关闭弹出框
      close() {
        this.addParam.visible = false;
        this.editParam.visible = false;
      },

      // 点击编辑按钮事件
      showEditView() {
        this.editParam.items = JSON.parse(JSON.stringify(this.paramItems));
        console.log(this.$refs["multipleTable"].data, "编辑事件");
        let data = this.$refs["multipleTable"].data;
        console.log("编辑修改", data);
        this.editParam.editForm = data;
        console.log(this.editParam.editForm);
        this.editParam.visible = true;
      },
      // 删除
      del() {
        console.log(this.$refs["multipleTable"].data, "删除事件");
        let data = this.$refs["multipleTable"].data;
        console.log("删除", data);
        this.$confirm(
            "此操作将永久删除证书：[" + data.name + "], 是否继续?",
            "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
          .then(() => {
            this.deleteRequest("/certificate/" + data.id).then((res) => {
              if (res) {
                console.log(res, "删除=======");
                this.$message.success(res.message);
                this.list();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      // 批量删除
      deleteMany() {
        this.selection = this.$refs["multipleTable"].selection;
        console.log(this.selection, "批量删除");
        if (this.selection.length == 0) {
          this.$message.warning("请选择后再删除");
          return;
        }
        this.$confirm(
            "此操作将永久删除 [" + this.selection.length + "]个职位, 是否继续?",
            "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
          )
          .then(() => {
            let ids = "?";
            this.selection.forEach((item) => {
              ids += "ids=" + item.id + "&";
            });
            console.log(ids, "ids===");
            this.deleteRequest("/certificate/" + ids).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message);
                this.list();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      // 添加按钮事件
      addBtn() {
        this.addParam.visible = true;
        this.addParam.items = JSON.parse(JSON.stringify(this.paramItems));
      },
      //确定添加
      addSave(item) {
        console.log("确定添加", item);
        //   保存
        this.postRequest("/certificate/", this.addParam.addForm).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.addParam.close(); //关闭
            this.addParam.addForm = {
              resume_id: '',
              name: '',
              file: '',
              obtain_date: '',
            };
            this.addParam.callback(); //重新查询
          }
        });
      },
      //修改确定
      doUpdate(item) {
        console.log(item);
        console.log("修改", this.editParam.editForm);
        this.putRequest("/certificate/", this.editParam.editForm).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.editParam.close(); //关闭

            this.editParam.callback(); //重新查询
          }
        });
      },
    },
  };
</script>
<style lang=""></style>